﻿using System;
using System.Collections.Generic;
using System.Linq;
using DC2010.Objects.Base;
using DC2010.Objects.Common;
using DC2010.Objects.Structures;

namespace DC2010.Objects.Data
{
    [Serializable]
    public class FloorInfo : BaseInfo
    {
	    /// <summary>
        /// Every map consists of several map floors. Each floor can be understand as one floor in game.
		/// each floor is created with several layers.
        /// </summary>		
        public List<Layer> Layers { get; set; }	
	
        public FloorInfo()
        {
            ////StartPosition = new Point(0, 0);
			
            //// there are several predefined layers
			
            //// Tiles
            //Layer lTiles = new Layer(BaseObjectType.Tile);
			
            //// Item on floor
            //Layer lItems = new Layer(BaseObjectType.Item);
			
            ////Party generally means monsters. Every monster must be in party.
            //Layer lParty = new Layer(BaseObjectType.Party);
			
            ////Door for current layer
            //Layer lDoors = new Layer(BaseObjectType.Door);

            Layers = new List<Layer>();
			
            //Layers.Add(lTiles);
            //Layers.Add(lItems);
            //Layers.Add(lParty);
            //Layers.Add(lDoors);
        }

        public Layer TileLayer 
        { 
            get
            {
                var x = (from i in Layers where i.Type == BaseObjectType.Tile select i).FirstOrDefault();
                if (x == null)
                {
                    x = new Layer(BaseObjectType.Tile);
                    Layers.Add(x);
                }

                return x;
            }
        }

	    /// <summary>
        /// This is simply a difficulty of a map
        /// </summary>		
        public int LayerMultiplier { get; set; }
		
        public int Width { get; set; }
        public int Height { get; set; }

        public FloorData CreateData()
        {
            FloorData cd = new FloorData();

            cd.InfoId = Id;
            cd.Id = cd.InfoId + "_" + Guid.NewGuid();
            cd.Layers = new List<Layer>(Layers);
            cd.TileLayer = (Layer)TileLayer.Clone();

            //instantiate
            foreach(KeyValue kv in cd.TileLayer.tableOfObjects)
            {
                TileInfo ti = Root.I.TileSystem.GetInfo(kv.Value);

                if (ti != null)
                {
                    switch (ti.TileType)
                    {
                        //case TileType.Teleport:
                        //    {
                        //        //we want to copy all relevant values.
                        //        //TileTeleportInfo tti = (TileTeleportInfo) ti;
                        //        //TileTeleportData ttd = (TileTeleportData) tti.CreateData();

                        //        //kv.Value = ttd.Id;
                        //        break;
                        //    }
                        default:
                            {
                                TileData td = ti.CreateData();
                                kv.Value = td.Id;
                                break;
                            }
                    }
                }
            }

            //TODO: fill with other properties
            Root.log.InfoFormat("Core, Created new object with id: {0} based on object {1}", cd.Id, cd.InfoId);
            Root.I.FloorSystem.AddData(cd);


            return cd;
        }


    }
}
